# 有5个具有5种不同颜色的房间；每个房间里分别住着一个不同国籍的人；每
# 个人都在喝一种特定品牌的饮料；抽一特定品牌的香烟；养一特定的宠物；没
# 有任意的两个人在抽相同品牌的烟、或喝相同品牌饮料、或养相同的宠物。
# 同时又知：
# 1.英国人住在红房子里；
# 2.瑞典人养狗；
# 3.丹麦人喝茶；
# 4.绿房子紧挨着白房子，在白房子的左边；
# 5.绿房子主人喝咖啡；
# 6.抽 PALL MALL牌香烟的人养鸟；
# 7.黄房子里的人抽 DUNHILL 牌的烟；
# 8.住中间房子的人喝牛奶；
# 9.挪威人住在第一个房子里（最左边）；
# 10.抽 BLENDS香烟的人和养猫的人相邻；
# 11.养马的人和抽 DUNHILL牌香烟的人相邻：
# 12.抽 BLUEMASTER牌香烟的人喝啤酒；
# 13.德国人抽 PRINCE牌香烟；
# 14.挪威人和住蓝房子的人相邻；
# 15.抽 BLENDS牌香烟的人和喝矿泉水的人相邻
# 问：谁在养鱼？
import time


# pets = [dog, bird, horse, cat, fish]
# nation = [British, Swede, Danish, Norwegian, German]
# cigarette = [PALL_MALL, DUNHILL, BLENDS, BLUEMASTER, PRINCE]
# drinks = [tea, coffee, milk, beer, water]
# house = [red, white, green, yellow, blue]


def close_to(x, y):
    return x == y - 1 or x == y + 1


def diff_5(x1, x2, x3, x4, x5):
    if x1 != x2 and x1 != x3 and x1 != x4 and x1 != x5 \
            and x2 != x3 and x2 != x4 and x2 != x5 \
            and x3 != x4 and x3 != x5 \
            and x4 != x5 and in_range(x1) and in_range(x2) and in_range(x3) and in_range(x4) and in_range(x5):
        return True
    return False


def in_range(x):
    return 5 >= x >= 1


Norwegian = 1
milk = 3
start = time.time()
for red in range(1, 6):
    British = red
    for white in range(1, 6):
        green = white - 1
        coffee = white - 1
        for yellow in range(1, 6):
            DUNHILL = yellow
            for blue in [Norwegian - 1, Norwegian + 1]:
                for tea in range(1, 6):
                    Danish = tea
                    for beer in range(1, 6):
                        BLUEMASTER = beer
                        for water in range(1, 6):
                            for PALL_MALL in range(1, 6):
                                bird = PALL_MALL
                                for BLENDS in [water - 1, water + 1]:
                                    for PRINCE in range(1, 6):
                                        German = PRINCE
                                        for Swede in range(1, 6):
                                            dog = Swede
                                            for horse in [DUNHILL - 1, DUNHILL + 1]:
                                                for cat in [BLENDS - 1, BLENDS + 1]:
                                                    for fish in range(1, 6):
                                                        if close_to(horse, yellow) \
                                                                and diff_5(dog, bird, horse, cat, fish) \
                                                                and diff_5(British, Swede, Danish, Norwegian, German) \
                                                                and diff_5(PALL_MALL, DUNHILL, BLENDS, BLUEMASTER,
                                                                           PRINCE) \
                                                                and diff_5(tea, coffee, milk, beer, water) \
                                                                and diff_5(red, white, green, yellow, blue):
                                                            print(
                                                                'dog %d, bird %d, horse %d, cat %d, fish %d' % (
                                                                    dog, bird, horse, cat, fish))
                                                            print(
                                                                ' British %d,Swede %d, Danish %d, Norwegian %d, German %d' % (
                                                                    British, Swede, Danish, Norwegian, German))
                                                            end = time.time()
                                                            print(end - start)
                                                            start = time.time()


